package com.szh.es.controller;

import com.szh.es.mapper.ProductMapper;
import com.szh.es.pojo.Product;
import com.szh.es.service.HotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping
@Slf4j
public class TestESController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    ProductMapper productMapper;

    @Resource
    HotelService hotelService;

    @GetMapping
    public void createIndex() {
        //创建索引，系统初始化会自动创建索引
        System.out.println("创建索引");
    }

    @DeleteMapping
    public void deleteIndex() {
        //创建索引，系统初始化会自动创建索引
        // boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);
        // System.out.println("删除索引 = " + flg);
    }

    @PostMapping
    public void save() {
        Product product = new Product();
        product.setId(1L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.atguigu/hw.jpg");
        productMapper.save(product);
    }

    @PutMapping
    public void update() {
        Product product = new Product();
        product.setId(1L);
        product.setTitle("小米 2 手机");
        product.setCategory("手机");
        product.setPrice(9999.0);
        product.setImages("http://www.atguigu/xm.jpg");
        productMapper.save(product);
    }


    @GetMapping("/findById")
    public void findById() {
        Product product = productMapper.findById(1L).get();
        System.out.println(product);
    }

    @GetMapping("/findAll")
    public void findAll() {
        Iterable<Product> products = productMapper.findAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }

    //删除
    @DeleteMapping("/delDocument")
    public void delete() {
        Product product = new Product();
        product.setId(1L);
        productMapper.delete(product);
    }

    //批量新增
    @PostMapping("/addBatch")
    public void saveAll() {
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i));
            product.setTitle("[" + i + "]小米手机");
            product.setCategory("手机");
            product.setPrice(1999.0 + i);
            product.setImages("http://www.atguigu/xm.jpg");
            productList.add(product);
        }
        productMapper.saveAll(productList);
    }

    //分页查询
    @GetMapping("/findByPageable")
    public void findByPageable() {
        //设置排序(排序方式，正序还是倒序，排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        int currentPage = 0;//当前页，第一页从 0 开始， 1 表示第二页
        int pageSize = 5;//每页显示多少条
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
        //分页查询
        Page<Product> productPage = productMapper.findAll(pageRequest);
        for (Product Product : productPage.getContent()) {
            System.out.println(Product);
        }
    }

    @GetMapping("/imporeES")
    public void imporeES() throws IOException {
        hotelService.imporeES();

    }

}
